#include <REGX52.H>
#include "oled.h"
#include "Timer0.h"
#include "Delay.h"
#include "music.h"
sbit DJ=P1^7;
sbit DWJ=P1^2;
unsigned char message,dtime;
unsigned char dcompare;
extern unsigned char enter[7];//输入 enter[0]为输入位置
extern unsigned char pass[7]; //初始密码为000000 pass[0]修改密码定位
extern char n;  	
void Uart_Init(void)		//9600bps@11.0592MHz
{
	PCON &= 0x7F;		//波特率不倍速
	SCON = 0x50;		//8位数据,可变波特率
	TMOD &= 0x0F;		//设置定时器模式
	TMOD |= 0x20;		//设置定时器模式
	TL1 = 0xFD;		//设置定时初始值
	TH1 = 0xFD;		//设置定时重载值
	ET1 = 0;		//禁止定时器1中断
	TR1 = 1;		//定时器1开始计时
	PS = 1;				//优先级高
	ES=1;					//打开串口中断
}


	
void control(unsigned char m){
	switch (m)
{
		case '1':
			if(enter[0]<6){
		enter[0]++;	  				
		enter[enter[0]] = 1;
		OLED_ShowNum(22+8*enter[0],1,enter[enter[0]],1,16);  //一位位显示输入密码
			}				
		break;
		case '2':
		if(enter[0]<6){
		enter[0]++;			
		enter[enter[0]] = 2;
			OLED_ShowNum(22+8*enter[0],1,enter[enter[0]],1,16);  //一位位显示输入密码
			}				
		break;
		case '3':
		if(enter[0]<6){
		enter[0]++;
			  				
		enter[enter[0]] = 3;
			OLED_ShowNum(22+8*enter[0],1,enter[enter[0]],1,16);  //一位位显示输入密码
			}				
		break;
		case '4':
		if(enter[0]<6){
		enter[0]++;
			  				
		enter[enter[0]] = 4;
			OLED_ShowNum(22+8*enter[0],1,enter[enter[0]],1,16);  //一位位显示输入密码
			}				
		break;
		case '5':
		if(enter[0]<6){
		enter[0]++;
			  				
		enter[enter[0]] = 5;
			OLED_ShowNum(22+8*enter[0],1,enter[enter[0]],1,16);  //一位位显示输入密码
			}				
		break;
		case '6':
		if(enter[0]<6){
		enter[0]++;
			  				
		enter[enter[0]] = 6;
			OLED_ShowNum(22+8*enter[0],1,enter[enter[0]],1,16);  //一位位显示输入密码
			}				
		break;
		case '7':
		if(enter[0]<6){
		enter[0]++;
			  				
		enter[enter[0]] = 7;
			OLED_ShowNum(22+8*enter[0],1,enter[enter[0]],1,16);  //一位位显示输入密码
			}				
		break;
		case '8':
		if(enter[0]<6){
		enter[0]++;
			  				
		enter[enter[0]] = 8;
			OLED_ShowNum(22+8*enter[0],1,enter[enter[0]],1,16);  //一位位显示输入密码
			}				
		break;
		case '9':
		if(enter[0]<6){
		enter[0]++;
			  				
		enter[enter[0]] = 9;
			OLED_ShowNum(22+8*enter[0],1,enter[enter[0]],1,16);  //一位位显示输入密码
			}				
		break;
		case '0':
		if(enter[0]<6){
		enter[0]++;
		
		enter[enter[0]] = 0;   								//输入0
			OLED_ShowNum(22+8*enter[0],1,enter[enter[0]],1,16);  //一位位显示输入密码
		}
		break;
		case 'D':
		if(enter[0]>=1){
		enter[enter[0]] = 0;
		OLED_ShowString(22+8*enter[0],1," ");  	//空白覆盖回退的一位
		enter[0]--;
		
		}
		break;
		case 'E':
		if(enter[0]==6){ 							//Enter确定开锁
			if(enter[1]==pass[1]&&enter[2]==pass[2]&&enter[3]==pass[3]&&enter[4]==pass[4]&&enter[5]==pass[5]&&enter[6]==pass[6])
			{
				OLED_ShowString(30,3,"Right!");
			dtime = 7;
			while(dtime){														//开锁舵机角度
			DJ=1;
			Delay500us();
			DJ=0;
			Delay(20);
			dtime--;
		}	
			}else{OLED_ShowString(30,3,"wrong!");
				
				
			}
			for(n=6;n--;n>=0){	
				enter[n]=0;   												//清零输入密码
			}	
			Delay(200);
			OLED_ShowString(30,1,"       ");				//清空密码输入oled
			OLED_ShowString(30,3,"       ");
		}
		break;
		case 'C':
		dtime=10;
		dcompare++;
		if(dcompare>5)dcompare=1;
		if(dcompare==1){
		while(dtime){
			DJ=1;
			Delay500us();
			DJ=0;
			Delay(20);
			dtime--;
		}
	}
		if(dcompare==2){
		while(dtime){
			DJ=1;
			Delay500us();
			Delay500us();
			DJ=0;
			Delay(19);
			dtime--;
		}
	}if(dcompare==3){
		while(dtime){
			DJ=1;
			Delay500us();
			Delay500us();
			Delay500us();
			DJ=0;
			Delay(19);
			dtime--;
		}
	}if(dcompare==4){
		while(dtime){
			DJ=1;
			Delay500us();
			Delay500us();
			Delay500us();
			Delay500us();
			DJ=0;
			Delay(18);
			dtime--;
		}
	}
		if(dcompare==5){
		while(dtime){
			DJ=1;
			Delay500us();
			Delay500us();
			Delay500us();
			Delay500us();
			Delay500us();
			DJ=0;
			Delay(18);
			dtime--;
		}
	}
		
		break;
		default:
		break;
}
}

void UART_Routine(void) interrupt 4
{

	RI=0;
	message=SBUF;
	control(message);
}



